package com.pswv2.backend.popularize.action;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.pswv2.backend.popularize.service.PopularizeService;
import com.pswv2.bub.bsc.dao.po.Popularize;
import com.pswv2.bub.bsc.dao.po.PopularizeImg;
import com.pswv2.util.BackendErrorCode;
import com.pswv2.util.CommonResp;


@Controller
@RequestMapping(value = "popularizeAction", produces = { "text/json;charset=UTF-8" })
public class PopularizeAction {

	
	@Autowired
	private PopularizeService popularizeService;
	
	/**
	 * 上传图片
	 * @return
	 */
	@ResponseBody
	@RequestMapping("uploadImg")
	public String uploadImg(int id,HttpServletRequest request, HttpSession session, HttpServletResponse response) {
		try {
			MultipartHttpServletRequest rq = (MultipartHttpServletRequest) request;
			Map<String, MultipartFile> file_list = rq.getFileMap();
			if (file_list != null && file_list.size() > 0) {
				if (file_list.containsKey("inputName")) {
					MultipartFile file = file_list.get("inputName");
					if (file != null) {
						String fileName = file.getOriginalFilename();
						System.out.println(fileName);
						File saveFile = new File("C:\\img\\" ,id+fileName);
						file.transferTo(saveFile);
						popularizeService.addPopularizeServiceImpl(id,"/imgUrl/"+id+fileName);
						return CommonResp.buildSuccessResp().toJsonString();
					}
				}
			}
			return CommonResp.buildErrorResp(BackendErrorCode.unknown_error).toJsonString();
		}catch(Exception e){
			e.printStackTrace();
			return CommonResp.buildErrorResp(BackendErrorCode.unknown_error).toJsonString();
		}
	}
	/**
	 * 通过标题查询
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryPRByTitle")
	public String queryPRByTitle(String title) {
		Map<String,Object> map=popularizeService.queryPRByTitle(title);
		return CommonResp.buildSuccessResp(map).toJsonString();
	}
	/**
	 * 通过标题查询
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryByTitle")
	public String queryByTitle(String title) {
		List<?> ls=popularizeService.queryByTitle(title);
		return CommonResp.buildSuccessResp(ls).toJsonString();
	}
	/**
	 * 修改描述详情
	 * @return
	 */
	@ResponseBody
	@RequestMapping("editByIdAll")
	public String editByIdAll(Integer popularizeId,String content,String address,String title,String keyDescribe1, String keyDescribe2, String keyDescribe3,
			String keyDescribe4, String keyDescribe5, String keyDescribe6, String keyDescribe7, String keyDescribe8){
		popularizeService.editById(  popularizeId,  content,  address,  title,  keyDescribe1,
				 keyDescribe2,  keyDescribe3,  keyDescribe4,  keyDescribe5,  keyDescribe6,
				 keyDescribe7,  keyDescribe8);
		return CommonResp.buildSuccessResp().toJsonString();
	}
	/**
	 * 修改描述详情
	 * @return
	 */
	@ResponseBody
	@RequestMapping("editByIdDescribe")
	public String editByIdDescribe(Integer popularizeId, String keyDescribe1, String keyDescribe2, String keyDescribe3,
			String keyDescribe4, String keyDescribe5, String keyDescribe6, String keyDescribe7, String keyDescribe8){
		popularizeService.editById( popularizeId,  keyDescribe1,  keyDescribe2,  keyDescribe3,
				keyDescribe4,  keyDescribe5,  keyDescribe6,  keyDescribe7,  keyDescribe8);
		return CommonResp.buildSuccessResp().toJsonString();
	}
	/**
	 * 通过用户id查询
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryByState")
	public String queryByState(Integer state) {
		List<Popularize> ls=popularizeService.queryByState(state);
		return CommonResp.buildSuccessResp(ls).toJsonString();
	}
	/**
	 * 通过popularizeId查询推广的图片
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryByPopularizeId")
	public String queryByPopularizeId(Integer popularizeId) {
		List<PopularizeImg> ls=popularizeService.queryByPopularizeId(popularizeId);
		return CommonResp.buildSuccessResp(ls).toJsonString();
	}
	/**
	 * 通过用户id查询
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryByUserId")
	public String queryByUserId(Integer userId) {
		List<?> ls=popularizeService.queryByUserId(userId);
		return CommonResp.buildSuccessResp(ls).toJsonString();
	}
	/**
	 * 修改状态
	 * @return
	 */
	@ResponseBody
	@RequestMapping("editState")
	public String editState(Integer popularizeId,Integer state) {
		popularizeService.editState(popularizeId,state);
		return CommonResp.buildSuccessResp().toJsonString();
	}
	
	/**
	 * 通过id查询
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryById")
	public String queryById(Integer popularizeId) {
		Popularize entity= popularizeService.queryById(popularizeId);
		return CommonResp.buildSuccessResp(entity).toJsonString();
	}
	/**
	 * 通过id删除
	 * @return
	 */
	@ResponseBody
	@RequestMapping("delById")
	public String delById(Integer popularizeId) {
		popularizeService.delById(popularizeId);
		return CommonResp.buildSuccessResp().toJsonString();
	}
	/**
	 * 通过id修改
	 * @return
	 */
	@ResponseBody
	@RequestMapping("editById")
	public String editById(Integer popularizeId,Integer territoryId,
			String title,String describe,String content) {
		popularizeService.editById(popularizeId, territoryId,
				 title, describe, content);
		return CommonResp.buildSuccessResp().toJsonString();
	}
	/**
	 * 添加并返回
	 * @return
	 */
	@ResponseBody
	@RequestMapping("add")
	public String add(Integer userId,Integer territoryId,
			String title,String address,String content) {
		Popularize entity=popularizeService.add(userId, territoryId,
				 title, address, content);
		return CommonResp.buildSuccessResp(entity.getPopularizeId()).toJsonString();
	}
	/**
	 * 查看全部
	 * @return
	 */
	@ResponseBody
	@RequestMapping("queryAll")
	public String queryAll() {
		List<Popularize> ls= popularizeService.queryAll();
		return CommonResp.buildSuccessResp(ls).toJsonString();
	}
}
